package top.zackyoung.bbs.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.zackyoung.bbs.controller.ThirdPartController;
import top.zackyoung.bbs.entity.Log;
import top.zackyoung.bbs.mapper.LogMapper;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: ZackYoung
 * @time: 2020/6/13
 */
@Aspect
@Component
public class UserAspect {
    @Pointcut("execution(* top.zackyoung.bbs.service.impl.UserServiceImpl.login(..))")
    public void pointcut() {
    }

    @Autowired
    HttpServletRequest req;

    @Autowired
    private LogMapper logMapper;

    @AfterReturning(value = "pointcut()", returning = "result")
    public void afterReturn(JoinPoint joinPoint, Object result) {
        Log log = new Log();
        Signature signature = joinPoint.getSignature();
        String method = signature.getName();
        Object[] args = joinPoint.getArgs();
        String ip = req.getRemoteAddr();
        log.setIp(ip);
        String ipAttr = new ThirdPartController().ipAttr(ip);
        log.setIpAttr(ipAttr);
        String actionArgs = "";
        for (Object j : args) {
            if (j == null) {
                continue;
            }
            actionArgs += j.toString() + " ";
        }
        log.setActionArgs(actionArgs);
        log.setActionMethod(method);
        String user = (String) args[0];
        log.setUsername(user);
        log.setType("登录");
        log.setReturnResult(result.toString());
        logMapper.insert(log);
    }


//    @Around("pointcut()")
//    public Object around(ProceedingJoinPoint joinPoint) {
//        Log log = new Log();
//        //获取方法的参数列表
//        Object[] args = joinPoint.getArgs();
//        Signature signature = joinPoint.getSignature();
//        String method = signature.getName();
//        String ip = req.getRemoteAddr();
//        log.setIp(ip);
//        String actionArgs = "";
//        for (Object j : args) {
//            if (j==null){
//                continue;
//            }
//            actionArgs += j.toString() + " ";
//        }
//        log.setActionArgs(actionArgs);
//        log.setActionMethod(method);
//        String ipAttr = new ThirdPartController().ipAttr(ip);
//        log.setIpAttr(ipAttr);
//        try {
//            if (method.equals("login")) {
//                String user = (String) args[0];
//                log.setUsername(user);
//                log.setType("登录");
//            }
//            if (method.equals("queryUser")) {
//                String user = (String) args[0];
//                log.setUsername(user);
//                log.setType("查询个人信息");
//            }
//            if (method.equals("updateAvatar")) {
//                log.setId((Integer) args[1]);
//                log.setType("更新头像");
//            }
//            if (method.equals("queryFocus")) {
//                log.setId((Integer) args[0]);
//                log.setType("查询关注数");
//            }
//            if (method.equals("queryFaceNum")) {
//                log.setUsername((String) args[1]);
//                log.setType("查询粉丝数");
//            }
//            if (method.equals("deleteFocus")) {
//                log.setId((Integer) args[0]);
//                log.setType("删除关注列表");
//            }
//            if (method.equals("insetFocus")) {
//                log.setId((Integer) args[0]);
//                log.setType("添加关注");
//            }
//            if (method.equals("queryUserLoginLog")) {
//                log.setId((Integer) args[0]);
//                log.setType("登录日志");
//            }
//            if (method.equals("queryUserLoginLogCount")) {
//                log.setId((Integer) args[0]);
//                log.setType("登录日志");
//            }
//            if (method.equals("insertKeep")){
//                log.setId((Integer) args[0]);
//                log.setType("加入收藏");
//            }
//            if (method.equals("queryAlreadyKeep")){
//                log.setId((Integer) args[0]);
//                log.setType("查询是否收藏");
//            }
//            log.setReturnResult(joinPoint.proceed(args).toString());
//            return joinPoint.proceed(args);
//        } catch (Throwable e) {
//            e.printStackTrace();
//            log.setException(e.getMessage());
//        } finally {
//            logMapper.insert(log);
//        }
//
//        return null;
//    }

}
